package com.amitg.phoneusetracker;


import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import java.util.Calendar;
import android.provider.CallLog;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.TextView;
import java.sql.Date;
public class PhoneUseTrackerActivity extends Activity {
    /** Called when the activity is first created. */
	public long getTodayTimestamp(){
        Calendar c1 = Calendar.getInstance();
        c1.add(Calendar.DATE, -1);
        //c1.setTime(new Date(0)); 

        Calendar c2 = Calendar.getInstance();
        c2.set(Calendar.YEAR, c1.get(Calendar.YEAR));
        c2.set(Calendar.MONTH, c1.get(Calendar.MONTH)-1);
        c2.set(Calendar.DAY_OF_MONTH, 6);
        //c2.set(Calendar.DAY_OF_MONTH, c1.get(Calendar.DAY_OF_MONTH));
        c2.set(Calendar.HOUR_OF_DAY, 0);
        c2.set(Calendar.MINUTE, 0);
        c2.set(Calendar.SECOND, 0);

        return c2.getTimeInMillis();
    }@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView call=(TextView)this.findViewById(R.id.call);
        call.setText("hi");
        StringBuffer sb = new StringBuffer();
        call.setText("hi1");
        

        String timestamp = String.valueOf(getTodayTimestamp());

        Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,null, CallLog.Calls.DATE + ">= ?", new String[]{timestamp} ,  CallLog.Calls.DATE +" DESC");
        int name = managedCursor.getColumnIndex( CallLog.Calls.CACHED_NAME );
        int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER ); 
        int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
        int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
        int duration = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
        sb.append( "Call Details :");
        call.setText("hi");
        int totalOGMins=0;
       while ( managedCursor.moveToNext() ) {
    	int durationMin=0;
    	String phName = managedCursor.getString( name );
    	String phNumber = managedCursor.getString( number );
        String callType = managedCursor.getString( type );
        String callDate = managedCursor.getString( date );
        Date callDayTime = new Date(Long.valueOf(callDate));
        String callDuration = managedCursor.getString( duration );
        durationMin=(int)Math.ceil(Integer.parseInt(callDuration)/60.0);
        String callDurationMin = Integer.toString(durationMin);
        String dir = null;
        int dircode = Integer.parseInt( callType );
        switch( dircode ) {
        case CallLog.Calls.OUTGOING_TYPE:
        dir = "OUTGOING";
        totalOGMins=totalOGMins+durationMin;
        break;

        case CallLog.Calls.INCOMING_TYPE:
        dir = "INCOMING";
        break;

        case CallLog.Calls.MISSED_TYPE:
        dir = "MISSED";
        break;
        }
        //sb.append( "\ntotal OG="+totalOGMins+"\nPhone Name:--- "+phName +"\nPhone Number:--- "+phNumber +" \nCall Type:--- "+dir+" \nCall Date:--- "+callDayTime+" \nCall duration in sec :--- "+callDuration+ " \nCall duration in minutes :--- "+callDurationMin);
        //sb.append("\n----------------------------------");
        }
        managedCursor.close();
        call.setMovementMethod(ScrollingMovementMethod.getInstance());
        sb.append( "\ntotal OG="+totalOGMins);
        call.setText(sb);
    }
}